#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <Array.au3>
#include <RecFileListToArray.au3>
#include <Misc.au3>
#include <File.au3>

_Singleton(@ScriptName, 0)

Dim $folderToRip, $fileSearch, $file, $convertUsingHandbrake, $autoripini, $MakeMKV, $finalMKVNewName, $finalMovieLocation, $iniFile, $handbrakePath
Dim $handbrakeCommandLine, $aRecords, $keepMKV, $movielocation, $newMovieLocation, $CountLines

Dim $ripListFile = (@ScriptDir & "/convertlist.log")

; Define the ini file based on the file version of this script (yes, it includes the .exe but
; this was the simplest way to handle it automatically
$autoripini = @ScriptDir & "\AutoRip2MKV.ini"

; Check to see if the ini file has the correct section.  If not, run the setup
; so that all the correct ini data can be populated.
$iniFile = IniReadSection($autoripini, "AutoRipSettings")

	$MakeMKV = $iniFile[1][1]
	$tempLocation = $iniFile[2][1]
	$finalMovieLocation = $iniFile[3][1]
	$sleepDelay = $iniFile[4][1]
	$convertAppleTV = $iniFile[5][1]
	$handbrakePath = $iniFile[6][1]
	$minTitleLength = $iniFile[7][1]
	$keepMKV = $iniFile[8][1]
	$handbrakeCommandLine = $iniFile[9][1]


Func _moveFilesToFinalLocation($movielocation, $newMovieLocation)
			$moveFiles = ShellExecute(@ScriptDir & "\CopyFiles.exe", $movielocation & " " & $newMovieLocation)
EndFunc   ;==>_moveFilesToFinalLocation


Func _convertToAppleTV($movielocation, $newMovieLocation)

	$aRecords = _RecFileListToArray($movielocation, "*.mkv", 1, 1, 1)

	If @error Then
		Return
	EndIf
	For $x = 1 To $aRecords[0]
		local $shortName = StringTrimRight ($aRecords[$x], 4)
		$convertUsingHandbrake = RunWait($handbrakePath & ' -i ' & '"' & $movielocation & '\' & $aRecords[$x] & '" -o ' & '"' & $movielocation & '\' & $shortName & $handbrakeCommandLine, '', @SW_MINIMIZE)

		If $keepMKV = 0 Then
			FileDelete($movielocation & '\' & $aRecords[$x])
		EndIf
	Next

	If  $tempLocation <> "" Then
		; move the files to the final location
		_moveFilesToFinalLocation($movielocation, $newMovieLocation)
	EndIf
EndFunc

Func _deleteLineFromLogFile($movielocation, $newMovieLocation)

	Local $aRecords

	If Not _FileReadToArray($ripListFile, $aRecords) Then
		Exit
	EndIf


	For $x = 1 To $aRecords[0]
		local $pathToCompareTo = $movielocation & "," & $newMovieLocation
		local $tempLogLine = FileReadLine ( $ripListFile , $x )
		If $pathToCompareTo = $tempLogLine Then
			_FileWriteToLine($ripListFile, $x, "",1)
		EndIf
	Next
EndFunc

Func _getMovieVariables()


	$CountLines = _FileCountLines($ripListFile)
	local $tempLogLine = FileReadLine ( $ripListFile , $CountLines )
	If $CountLines < 1 Then
		Exit
	EndIf
	local $splitString = StringSplit ($tempLogLine, ",")
	;_ArrayDisplay($splitString, 'Default Search')
	$movielocation = $splitString[1]
	$newMovieLocation= $splitString[2]
EndFunc

$CountLines = 1

While $CountLines > 0

	_getMovieVariables()

	Sleep(5000)

	_convertToAppleTV($movielocation, $newMovieLocation)
	Sleep(5000)
	_moveFilesToFinalLocation($movielocation, $newMovieLocation)

	Sleep(5000)
	;Delete the line of text that we just completed ripping
	_deleteLineFromLogFile($movielocation, $newMovieLocation)
	Sleep(5000)
WEnd

Exit